﻿using DeHeng_Mes.Dao;
using DeHeng_Mes.Dao.PlcDao;
using DeHeng_Mes.Model.PlcModel.door_02;
using DeHeng_Mes.Model.Table;
using DeHeng_Mes.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeHeng_Mes.Thread
{
    public class RealTimeThread
    {
        PlcDao plcDao = new PlcDao();

        ProductionPowerDao productionDao = new ProductionPowerDao();

        public async Task HourProduction()
        {
            Tools.ProduceLogText($"小时产量采集线程启动成功！");
            try
            {
                while ( true )
                {

                    await Task.Delay(5000);
                    List<Model.PlcModel.PlcConfigModel> plcContainer = PlcConnectionManager.plcContainer;

                    foreach (var plcModel in plcContainer)
                    {
                        // 未连接，跳过
                        if (!plcModel.isConnected)
                            continue;

                        string plcName = plcModel.name;

                        if (!plcName.Equals("DOOR-02"))
                            continue;

                        // 获取Plc数据实例与实例类型
                        Dictionary<object, Type> obj = MappingTable.PlcMapping(plcModel.plc, plcName);

                        // 获取故障数据
                        Dictionary<string, object> hourProductionData = plcDao.GetFieldValues(obj, "Production");

                        // 获取当前小时
                        string hour = DateTime.Now.Hour.ToString();

                        foreach (var hourProduction in hourProductionData)
                        {
                            if (hourProduction.Key.Contains("RealTimeProduction"))
                                continue;

                            // 获取当前数据的方向
                            string position = hourProduction.Key[0].ToString();

                            // 获取当前数据表示的小时
                            string realHour = hourProduction.Key.Replace(position + "Time", "");

                            // 获取当前日期
                            DateTime currentDate = DateTime.Today;

                            // 获取当前产量
                            string realProduction = hourProduction.Value.ToString();

                            // 包装数据
                            TraceHourProduction traceHourProduction = new TraceHourProduction()
                            {
                                Date = currentDate,
                                FProduction = realProduction,
                                Hour = realHour,
                                PlcName = plcName,
                                LR = position,
                            };

                            // 读取数据
                            string production = productionDao.GetHoutProduction(traceHourProduction);

                            if (!string.IsNullOrEmpty(production) && int.Parse(realProduction) > int.Parse(production))
                            {
                                // 执行更新
                                productionDao.UpdateHourProduction(traceHourProduction, realProduction);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tools.ProduceLogTextError($"小时产量采集失败，原因是{ex.Message}");
            }
        }
    }
}
